package com.lottery.shadowbd.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lottery.shadowbd.R;
import com.lottery.shadowbd.bean.HistoryItem;
import com.lottery.shadowbd.bean.LotteryHistory;
import com.lottery.shadowbd.bean.TestNews;
import com.lottery.shadowbd.callback.OnMyItemClickListener;
import com.lottery.shadowbd.utils.LotteryUtils;
import com.lottery.shadowbd.widget.KJLineItemView;

import java.util.List;

import static android.R.attr.id;
import static android.R.id.list;

/**
 * Created by Administrator on 2017/4/1 0001.
 */
public class LotHistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<LotteryHistory.ListEntity> mList;

    private OnMyItemClickListener myItemClickListener;

    private String id;

    public LotHistoryAdapter(Context mContext, List<LotteryHistory.ListEntity> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_lot_history, parent, false);

        ViewHolder holder = new ViewHolder(mView);

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder mHolder = (ViewHolder) holder;

        LotteryHistory.ListEntity o = mList.get(position);
        HistoryItem item = LotteryUtils.convertHistoryItem(id, o);

        if (item == null) {
            mHolder.time.setText("开奖日期" + LotteryUtils.getDate(o.getEndTime()));
            mHolder.phase.setText("第" + o.getIssue() + "期");
            mHolder.red.setText(o.getWinNumber());
            mHolder.blue.setText(o.getBallNumber());
        } else {
            mHolder.time.setText("开奖日期" + LotteryUtils.getDate(item.getTime()));
            mHolder.phase.setText("第" + item.getIssue() + "期");
            mHolder.red.setText(item.getRed());
            mHolder.blue.setText(item.getBlue());
        }

        if (myItemClickListener != null) {
            mHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myItemClickListener.onItemClick(v, position);
                }
            });
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList != null ? mList.size() : 0;
    }

    public void setMyItemClickListener(OnMyItemClickListener myItemClickListener) {
        this.myItemClickListener = myItemClickListener;
    }

    public void setId(String id) {
        this.id = id;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView time;
        TextView phase;
        TextView red;
        TextView blue;

        public ViewHolder(View itemView) {
            super(itemView);

            time = (TextView) itemView.findViewById(R.id.text_history_timedraw);
            phase = (TextView) itemView.findViewById(R.id.text_history_phase);

            red = (TextView) itemView.findViewById(R.id.text_history_red);
            blue = (TextView) itemView.findViewById(R.id.text_history_blue);
        }
    }
}
